//
//  RegisterThreeViewController.m
//  HTP
//
//  Created by 陈钱钿 on 15/11/4.
//  Copyright © 2015年 nit. All rights reserved.
//

#import "RegisterThreeViewController.h"
#import "ProfileModel.h"
#import "UserVM.h"
#import "UIAlertView+Block.h"
#import "AppDelegate.h"
#define kBorder 20
@interface RegisterThreeViewController ()
@property (nonatomic,retain) UITextField *pwd;
@property (nonatomic,retain) UITextField *confirmPwd;
@end

@implementation RegisterThreeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
  
    
    [self initUI];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self addCustomTitleWithTitle:@"设置密码"];
    [self addCustomBackBase];
    
}
//TODO:绘制UI
- (void)initUI {
    _pwd = [[UITextField alloc] initWithFrame:CGRectMake(kBorder, 2*kBorder, ScreenWidth-2*kBorder, 38)];
    _pwd.borderStyle = UITextBorderStyleRoundedRect;
    _pwd.placeholder = @"请输入密码";
    _pwd.secureTextEntry = YES;
    [self.view addSubview:_pwd];
    
    _confirmPwd = [[UITextField alloc] initWithFrame:CGRectMake(kBorder, CGRectGetMaxY(_pwd.frame)+2*kBorder, ScreenWidth-2*kBorder, 38)];
    _confirmPwd.borderStyle = UITextBorderStyleRoundedRect;
    _confirmPwd.placeholder = @"请再次输入密码";
    _confirmPwd.secureTextEntry = YES;
    
    [self.view addSubview:_confirmPwd];
    
    UIButton *confirmBtn = [[UIButton alloc] initWithFrame:CGRectMake(kBorder, CGRectGetMaxY(_confirmPwd.frame)+2*kBorder, ScreenWidth-2*kBorder, 35)];
    [confirmBtn setBackgroundImage:[UIImage imageNamed:@"confirm2"] forState:UIControlStateNormal];
    [confirmBtn addTarget:self action:@selector(userRegister) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:confirmBtn];
}
/**
 *  注册动作
 */
-(void)userRegister{
    if (![_pwd.text isEqualToString:_confirmPwd.text]) {
        [self showSVProgressFailed:@"密码不一致"];
        return;
    }
    if (_pwd.text.length < 6) {
        [self showSVProgressFailed:@"密码长度不能小于6位"];
        return;
    }
    UserVM *userVM = [[UserVM alloc] init];
    [userVM setBlockWithReturnBlock:^(id returnValue) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"青苗家教" message:@"注册成功" delegate:nil cancelButtonTitle:@"立即登录" otherButtonTitles:nil, nil];
        [alert showAlertViewWithCompleteBlock:^(NSInteger buttonIndex) {
            [self loginAction];
        }];
    } WithErrorBlock:^(id errorCode) {
        [self showSVProgressFailed:@"用户名已注册"];
    } WithFailureBlock:^{
        [self showSVProgressFailed:INTERFACE_FAILURE_CHIN];
    }];
    [self showSvprogress:@""];
    [userVM userRegister:_userName andPwd:_pwd.text];
}
-(void)loginAction{
    UserVM *userVM = [[UserVM alloc] init];
    [userVM setBlockWithReturnBlock:^(id returnValue) {
        [self dismissSVProgress];
        NSDictionary *dict = returnValue[INTERFACE_INFO];
        [USERCONTENT setUserBean:dict];
        [USERCONTENT setUserName:_userName];
        [self changeRoot];
    } WithErrorBlock:^(id errorCode) {
        [self showSVProgressFailed:@"登录失败"];
        [self.navigationController popToRootViewControllerAnimated:YES];
    } WithFailureBlock:^{
        [self showSVProgressFailed:INTERFACE_FAILURE_CHIN];
        [self.navigationController popToRootViewControllerAnimated:YES];
    }];
    [userVM userLogin:_userName andPwd:_pwd.text withUserType:@"2"];
}
/*
 更改RootViewController
 */
-(void)changeRoot{
    AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [app changeRootVc];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
